package com.example.app;
import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class HomeMenuItemAdapter extends RecyclerView.Adapter<HomeMenuItemAdapter.ViewHolder> {
    private final List<HomeMenuItem> mHomeMennuList;
    private OnMenuItemClickListener mListener;
    public void setOnMenuItemClickListener(OnMenuItemClickListener listener) {
        mListener = listener;
    }
    static class ViewHolder extends RecyclerView.ViewHolder{
        View newsTypeView;
        ImageView itemImage;
        TextView itemName;

        public ViewHolder(View view){
            super(view);
            newsTypeView = view;
            itemImage = view.findViewById(R.id.menu_icon);
            itemName = view.findViewById(R.id.menuitem_name);
        }
    }

    public HomeMenuItemAdapter(List<HomeMenuItem> list){
        this.mHomeMennuList = list;
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_menu_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        HomeMenuItem item = mHomeMennuList.get(position);
        holder.itemImage.setImageResource(item.imageId);
        holder.itemName.setText(item.name);
        holder.newsTypeView.setOnClickListener(v -> {
            if(mListener != null)
                mListener.onMenuItemClick(position);
        });

    }

    @Override
    public int getItemCount() {
        return mHomeMennuList.size();
    }

    public interface OnMenuItemClickListener {
        void onMenuItemClick(int position);
    }
}
